﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TestResultsLogging.Models;

using log4net;

namespace TestResultsLogging.Controllers
{
    public class TestCaseInsertController : Controller
    {
        private TestCaseDBInteraction _db = new TestCaseDBInteraction();
        private TestResultsDBEntities _entities = new TestResultsDBEntities();
        private ILog log = LogManager.GetLogger(typeof(HomeController));
        
        //
        // GET: /TestCaseInsert/

        public ActionResult TestCase()
        {
            try
            {
                log.Debug("Entering TestCaseInsertController.TestCase Returning Test Case " + this.ToString());
                return View(_entities.TestCase_vw.ToList());
            }
            catch (Exception e)
            {
                log.Error("Exception", e);
                ViewData["message"] = "Exception: " + e.Message;
                return View("Error");
            }

        }

        //
        // GET: /TestCaseInsert/Details/5

        public ActionResult Details(int id)
        {
            try
            {
                log.Debug("Entering TestCaseInsertController.Details Test Case List " + this.ToString());
                TestCase_vw individualTestCase = (from t in _entities.TestCase_vw
                                                  where t.TestCaseId_pk == id
                                                  select t).First();

                return View(individualTestCase);
            }

            catch (Exception e)
            {
                log.Error("Exception", e);
                ViewData["message"] = "Exception: " + e.Message;
                return View("Error");
            }
        }

        //
        // GET: /TestCaseInsert/Create

        public ActionResult Create()
        {
            string msg = "Create requires parameters";
            log.Error(msg);
            ViewData["message"] = msg;
            return View("Error");
        } 

        //
        // POST: /TestCaseInsert/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create([Bind (Exclude = "TestCaseId_pk,CreatedDateTime,CreatedBy,ModifiedDateTime,ModifiedBy")] TestCase_tbl testcaseToCreate)
        {
            try
            {
                log.Debug("Entering TestCaseInsertController.Create " + this.ToString());
                // Not sure if this should be handled in the UI instead but am putting the value checks here
                bool testCaseTypeIdExists =_db.testcaseTypeIdExists(testcaseToCreate.TestCaseTypeId_fk);
                if (!testCaseTypeIdExists)
                {
                    string msg = "Incorrect Test Case Type Id: " + testcaseToCreate.TestCaseTypeId_fk;
                    log.Error(msg);
                    ViewData["message"] = msg;
                    return View("Error");
                }

                bool testApplicationIdExists = _db.testApplicationIdExists(testcaseToCreate.TestApplicationId_fk);
                if (!testApplicationIdExists)
                {

                    string msg = "Incorrect Application Id: " + testcaseToCreate.TestEnvironmentId_fk;
                    log.Error(msg);
                    ViewData["message"] = msg; 
                    return View("Error");
                }

                bool testEnvironmentIdExists = _db.testEnvironmentIdExists(testcaseToCreate.TestEnvironmentId_fk);
                if (!testEnvironmentIdExists)
                {
                    string msg = "Incorrect Environment Id: " + testcaseToCreate.TestEnvironmentId_fk;
                    log.Error(msg);
                    ViewData["message"] = msg;
                    return View("Error");
                }

                int success = _db.insertTestCase(testcaseToCreate);
                if (success < 1)
                {
                    string msg = "Test Case failed to insert";
                    log.Error(msg);
                    ViewData["message"] = msg;
                    return View("Error");
                }

                log.Info("Test case " + success + " created");
                return RedirectToAction("TestCase");
            }
            catch (Exception e)
            {
                log.Error("Exception", e);
                ViewData["message"] = "Exception: " + e.Message;
                return View("Error");
            }
        }
        
        //
        // GET: /TestCaseInsert/Edit/5
 
        public ActionResult Edit(int id)
        {
            try
            {
                log.Debug("Entering TestCaseInsertController.Edit " + this.ToString());
                TestCase_tbl editTestcase = (from t in _entities.TestCase_tbl
                                             where t.TestCaseId_pk == id
                                             select t).First();
                return View(editTestcase);
            }

            catch (Exception e)
            {
                log.Error("Exception", e);
                ViewData["message"] = "Exception: " + e.Message;
                return View("Error");
            }
        }

        //
        // POST: /TestCaseInsert/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(TestCase_tbl testcaseToEdit)
        {
            DateTime dt = DateTime.Now;
            try
            {
                log.Debug("Entering TestCaseInsertController.Edit with TestCase Object " + this.ToString());
                // Not sure if this should be handled in the UI instead but am putting the value checks here
                bool testCaseIdExists = _db.testCaseExists(testcaseToEdit.TestCaseId_pk);
                if (!testCaseIdExists)
                {
                    string msg = "TestCase Id " + testcaseToEdit.TestCaseId_pk + " does not exist";
                    log.Error(msg);
                    ViewData["message"] = msg;
                    return View("Error");
                }

                bool testCaseTypeIdExists = _db.testcaseTypeIdExists(testcaseToEdit.TestCaseTypeId_fk);
                if (!testCaseTypeIdExists)
                {
                    string msg = "Test Case Type " + testcaseToEdit.TestCaseTypeId_fk + " does not exist";
                    log.Error(msg);
                    ViewData["message"] = msg; 
                    return View("Error");
                }

                bool testApplicationIdExists = _db.testApplicationIdExists(testcaseToEdit.TestApplicationId_fk);
                if (!testApplicationIdExists)
                {
                    string msg = "Test Application " + testcaseToEdit.TestApplicationId_fk + " does not exist";
                    log.Error(msg);
                    ViewData["message"] = msg;
                    return View("Error");
                }

                bool testEnvironmentIdExists = _db.testEnvironmentIdExists(testcaseToEdit.TestEnvironmentId_fk);
                if (!testApplicationIdExists)
                {
                    string msg = "Test Environment " + testcaseToEdit.TestEnvironmentId_fk + " does not exist";
                    log.Error(msg);
                    ViewData["message"] = msg;
                    return View("Error");
                }

                int success = _db.updateTestCase(testcaseToEdit);

                if (success < 1)
                {
                    string msg = "Test Case Failed to Update";
                    log.Error(msg);
                    ViewData["message"] = msg;
                    return View("Error");
                }

                log.Info("Test Case " + testcaseToEdit.TestCaseId_pk.ToString() + "updated");
                return RedirectToAction("TestCase");
            }
            catch(Exception e)
            {
                log.Error("Exception", e);
                ViewData["message"] = "Exception: " + e.Message;
                return View("Error");
            }
        }

        //
        // GET: /TestCaseInsert/Delete/5
 
        public ActionResult Delete(int id)
        {
            string msg = "Delete not implemented";
            log.Error(msg);
            ViewData["message"] = msg;
            return View("Error");
        }

        //
        // POST: /TestCaseInsert/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                string msg = "Delete not implemented";
                log.Error(msg);
                ViewData["message"] = msg;
                return View("Error");
            }
            catch(Exception e)
            {
                log.Error("Exception", e);
                ViewData["message"] = "Exception: " + e.Message;
                return View("Error");
            }
        }
    }
}
